import 'package:flutter_douban/model/note_model.dart';
import 'package:sqflite/sqlite_api.dart';
import 'base_db_provider.dart';

class UserDbProvider extends BaseDbProvider {
  /// 表名
  final String name = 'UserInfo';

  /// 用户电话
  final String phoneNum = "phoneNum";

  /// 用户密码
  final String codeNum = "codeNum";

  /// 用户昵称
  final String nickName = "nickName";

  /// 用户性别
  final String sex = "sex";

  /// 用户生日
  final String birthday = "birthday";

  /// 用户职业
  final String profession = "profession";

  /// 用户家乡
  final String hometown = "hometown";

  /// 用户签名
  final String signature = "signature";
  UserDbProvider();
  @override
  tableName() {
    return name;
  }

  /// 创建表
  @override
  createTableString() {
    return '''
        create table $name (
        $phoneNum text,$codeNum varchar(64),
        $nickName text,$sex text,$birthday text,$profession text,$hometown text,$signature text)
      ''';
  }

  /// 查询数据库
  Future getUserProvider(Database db, String phone) async {
    List<Map<String, dynamic>> maps =
        await db.rawQuery("select * from $name where $phoneNum = $phone");
    return maps;
  }

  /// 根据电话查询账户
  Future getUserData(String phone) async {
    Database db = await getDataBase();
    List<Map<String, dynamic>> maps =
    await db.rawQuery("select * from $name where $phoneNum = $phone");
    return maps;
  }

  /// 插入到数据库
  Future insert(String phone, String code) async {
    Database db = await getDataBase();
    var noteProvider = await getUserProvider(db, phone);
    if (noteProvider != null) {
      await db.delete(name, where: "$phoneNum = ?", whereArgs: [phone]);
    }
    return await db.rawInsert(
        "insert into $name ($phoneNum,$codeNum,$nickName,$sex,$birthday,$profession,$hometown,$signature) values (?,?,?,?,?,?,?,?)",
        [phone, code, '', '', '', '', '', '']);
  }

  /// 更新数据库
  /// phoneNum codeNum  nickName sex birthday profession hometown signature
  Future<void> update(
    String phone,
    String nick,
    String seex,
    String birtday,
    String profssion,
    String home,
    String signatur,
  ) async {
    Database database = await getDataBase();
    await database.rawUpdate(
        "update $name set $phoneNum = ?,$nickName = ?,$sex = ?,$birthday = ?,$profession = ?,$hometown = ?,$signature = ?",
        [phone, nick, seex, birtday, profssion, home, signatur]);
  }
}
